package com.ihunanren.biz.function.domain;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.google.common.collect.Lists;
import com.ihunanren.biz.weixin.domain.school.Division;
import com.ihunanren.common.CalendarUtil;
import com.ihunanren.common.CollectionUtils;
import com.ihunanren.dao.pojo.SchoolCurriculumDefinedDO;
import com.ihunanren.dao.pojo.SchoolCurriculumInfoDO;

public class SchoolCurriculum extends WeixinResp{

	private final SchoolCurriculumDefinedDO curriculumDefinedDO;
	
	private final List<SchoolCurriculumInfoDO> schoolCurriculumInfoList;

	public SchoolCurriculum(SchoolCurriculumDefinedDO curriculumDefinedDO,
			List<SchoolCurriculumInfoDO> schoolCurriculumInfoList) {
		this.curriculumDefinedDO = curriculumDefinedDO;
		this.schoolCurriculumInfoList = schoolCurriculumInfoList;
	}

	@Override
	public String toReply() {
		StringBuilder sb = new StringBuilder();
		if(curriculumDefinedDO == null){
			return "你是要查询课程表吗？非常抱歉根据你的信息检索不到你查询结果";
		}
		
		Division division = new Division(curriculumDefinedDO.getSchoolId());
		sb.append("『学校专业班级信息』").append("\n学校：")
				.append(division.getSchoolName()).append("\n专业：")
				.append(curriculumDefinedDO.getDeptName()).append("\n班级：")
				.append(curriculumDefinedDO.getClassName());
		
		Time time = new Time("北京",new Date());
		
		if(CollectionUtils.isEmpty(schoolCurriculumInfoList)){
			sb.append("\n\n『日期时间』\n").append(time.toReply());
			sb.append("\n\n『课程表信息』暂无该大学的课程表信息，添加大学课程表信息请前往www.ihunanren.com进行添加");
		}else{
			sb.append("\n\n『日期时间』\n").append(time.toReply());
			sb.append("\n\n『课程表信息』\n");
			SchoolCurriculumInfoDO currentLession = getCurrentLession();
			if(currentLession != null){
				sb.append("这个时间正是今天的第")
						.append(currentLession.getLessionSection())
						.append("节课时间，你应该在“")
						.append(currentLession.getLocation()).append("”上“")
						.append(currentLession.getTeacher()).append("”老师的《")
						.append(currentLession.getLession()).append("》")
						.append("，若还没去赶紧去签个到吧，挂科补考也是件麻烦事！");
			}else{
				SchoolCurriculumInfoDO nextLession = getNextTimeLession();
				if(nextLession != null){
					String str = String.format(next,
							nextLession.getLessionSection(),
							nextLession.getLocation(),
							nextLession.getTeacher(), nextLession.getLession(),
							nextLession.getTimeStart(),
							nextLession.getTimeEnd());
					sb.append(str);
				}else{
					sb.append("这个时间点没有课了，该谈恋爱的谈恋爱，该玩Dota的玩Dota！");
				}
			}
			sb.append("\n\n『志愿者信息』\n以上信息由该校志愿者『")
					.append(curriculumDefinedDO.getName())
					.append("』提供，让我们对ta的奉献精神致以崇高的敬意。若信息有误，请联系志愿者更改，或自己贡献课程表，中国高校课程表提交网址:www.ihunanren.com");
		}
		return sb.toString();
	}
	
	private String next = "离现在时间最近的一节课是今天的第%s节课\n上课地点：%s\n授课老师：%s\n专业课程：%s\n上课时间：%s-%s";
	
	private int getWeek() {
		int week = CalendarUtil.getDay(new Date()) - 1;
		return week == 0 ? 7 : week;
	}
	
	private SchoolCurriculumInfoDO getCurrentLession(){
		int week = getWeek();
		for(SchoolCurriculumInfoDO infoDO : schoolCurriculumInfoList){
			if(infoDO.getWeek() != week){
				continue;
			}
			String str = CalendarUtil.toString(new Date(), CalendarUtil.DATE_FMT_3);
			String start = str+" "+infoDO.getTimeStart();
			String end = str+" "+infoDO.getTimeEnd();
			Date s = CalendarUtil.toDate(start, "yyyy-MM-dd HH:mm");
			Date e = CalendarUtil.toDate(end, "yyyy-MM-dd HH:mm");
			
			if(CalendarUtil.isDateBetween(new Date(), s, e)){
				return infoDO;
			}
		}
		return null;
	}
	
	private SchoolCurriculumInfoDO getNextTimeLession(){
		int week = getWeek();
		List<StartDate> list = Lists.newArrayList();
		for(SchoolCurriculumInfoDO infoDO : schoolCurriculumInfoList){
			if(infoDO.getWeek() != week){
				continue;
			}
			String str = CalendarUtil.toString(new Date(), CalendarUtil.DATE_FMT_3);
			String start = str+" "+infoDO.getTimeStart();
			Date s = CalendarUtil.toDate(start, "yyyy-MM-dd HH:mm");
			int num = CalendarUtil.compareTwoDate(new Date(), s);
			if(num == CalendarUtil.BEFORE || num == CalendarUtil.EQUAL){
				list.add(new StartDate(s, infoDO));
			}
		}
		if(CollectionUtils.isNotEmpty(list)){
			Collections.sort(list);
			return list.get(0).getCurriculumInfoDO();
		}
		return null;
	}
	
	private class StartDate implements Comparable<StartDate>{
		private Date date;
		private SchoolCurriculumInfoDO curriculumInfoDO;
		
		public StartDate(Date date, SchoolCurriculumInfoDO curriculumInfoDO) {
			this.date = date;
			this.curriculumInfoDO = curriculumInfoDO;
		}

		@Override
		public int compareTo(StartDate o) {
			int num = CalendarUtil.compareTwoDate(date, o.getDate());
			if(num == CalendarUtil.BEFORE){
				return -1;
			}
			return 1;
		}

		public Date getDate() {
			return date;
		}

		public SchoolCurriculumInfoDO getCurriculumInfoDO() {
			return curriculumInfoDO;
		}
	}
}
